package org.example.service.bank.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.common.util.JwtUtils;
import org.example.controller.rep.BankQuesListRep;
import org.example.controller.rep.BankRatePageRep;
import org.example.controller.req.BankQuestionDetectionReq;
import org.example.dao.bank.BankQuestionDao;
import org.example.dao.bank.BankQuestionResultDao;
import org.example.entity.bank.BankQuestionEntity;
import org.example.entity.bank.BankQuestionResultEntity;
import org.example.service.bank.BankQuestionService;
import org.example.service.utils.Constant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class BankQuestionServiceImpl extends ServiceImpl<BankQuestionDao, BankQuestionEntity> implements BankQuestionService {

    @Resource
    private BankQuestionResultDao bankQuestionResultDao;
    @Override
    public List<BankQuesListRep> questionList() {
        List<BankQuesListRep> list = new ArrayList<>();
        List<BankQuestionEntity> bankQuestionEntities = baseMapper.selectList(null);
        bankQuestionEntities.forEach(obj->{
            BankQuesListRep bankQuesListRep =  BankQuesListRep.builder()
                    .id(obj.getId())
                    .bankId(obj.getBankId())
                    .question(obj.getQuestion())
                    .questionType(obj.getQuestionType())
                    .questionOption(obj.getQuestionOption())
                    .sequence(obj.getSequence())
                    .build();
            list.add(bankQuesListRep);
        });
        return list;
    }

    @Override
    public void detection(BankQuestionDetectionReq bankQuestionDetectionReq) {
        Long id = JwtUtils.getUserInfo().getId();
        int maxBatchByUser = bankQuestionResultDao.getMaxBatchByUser(id.intValue());
        List<BankQuestionEntity> bankQuestionEntities = baseMapper.selectList(null);
        bankQuestionDetectionReq.getQuestionDetections().forEach(obj->{
            Optional<BankQuestionEntity> first = bankQuestionEntities.stream().filter(o -> o.getId().equals(Long.valueOf(obj.getQuesId()))).findFirst();
            BankQuestionResultEntity entity = new BankQuestionResultEntity();
            entity.setBankTestQuestionId(Integer.valueOf(obj.getQuesId()));
            entity.setResContent(obj.getAnswerId());
            entity.setUserId(id.intValue());
            entity.setBatchNum(maxBatchByUser);
            if (first.isPresent()){
                BankQuestionEntity bankQuestionEntity = first.get();
                if (bankQuestionEntity.getQuestionOptionResult().equals(obj.getAnswerId())){
                    entity.setResult(Constant.ONE);
                }else{
                    entity.setResult(Constant.ZERO);
                }
            }else {
                entity.setResult(Constant.ZERO);
            }

            bankQuestionResultDao.insert(entity);

        });
            log.info("请求数据:{}", JSONObject.toJSONString(bankQuestionDetectionReq));
    }
}
